package com.mrobuy.jdvc.controller;

import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.category.VCCategotyLoader;
import com.mrobuy.jdvc.Result;
import com.mrobuy.util.Pagination;
import com.mrobuy.jdvc.dao.VCCategotyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller()
public class CateogryController {

	@Autowired
	private VCCategotyDao vcCategotyDao;

	@RequestMapping("/category")
	public String category(Model model) {

//		List<VCCategory> categories = vcCategotyDao.list();
//
//		model.addAttribute("categories",categories);

		return "category";
	}


	@RequestMapping("/category/query")
	@ResponseBody
	public Object query(HttpServletRequest request) {

		int start = ServletRequestUtils.getIntParameter(request, "start", 1);
		int length = ServletRequestUtils.getIntParameter(request, "length", 10);

		int draw = ServletRequestUtils.getIntParameter(request, "draw", 1);


		Pagination pagination = vcCategotyDao.load(start, length);

		//int offset = page*pagesize - pagesize;
		pagination.setDraw(draw);

		return pagination;
	}

	@RequestMapping("/category/sync")
	@ResponseBody
	public Result categorysync() {

		Result result = new Result(true);

		try {
			VCCategotyLoader.load(JdClientFactory.jdClient(), "wysgyp");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(e.getMessage());
		}

		return result;
	}
}
